package com.ldc.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ldc.api.common.ParamsHolder;
import com.ldc.model.SysRole;
import com.ldc.model.base.ApiBean;
import com.ldc.model.params.PageParams;
import com.ldc.service.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/17
 */
@RestController
@RequestMapping("/admin")
public class RoleManagerController {
    @Autowired
    RoleService roleService;


    @PostMapping("/role/new")
    public ApiBean<?> roleNew(@RequestBody SysRole params) {
        if (!StringUtils.hasLength(params.getRole()) || !StringUtils.hasLength(params.getRoleName())) {
            return ApiBean.failed("参数不合法");
        }
        SysRole sysRole = roleService.findByRole(params.getRole());
        if (sysRole == null) {
            sysRole = ParamsHolder.createNewRole(params.getRole()
                    , params.getRoleName(), params.getRemark());
            int code = roleService.getBaseMapper().insert(sysRole);
            return ApiBean.success(code > 0);
        }
        return ApiBean.failed("该角色已经存在");
    }

    /**
     * 权限列表
     *
     * @param params
     * @return
     */
    @PostMapping("/role/list")
    public ApiBean<?> roleList(@RequestBody PageParams params) {
        IPage<SysRole> record = roleService.findPageByPageQuery(params.getPage(), params.getOffset(), params.getQuery());
        if (record == null) {
            return ApiBean.failed("数据为空");
        }
        return ApiBean.successPage(record);
    }

}
